package Codec;

public class Codec {
      public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
          // 前序遍历
        if(root == null){
            return "nullP";
        }
        String ans = "";
        if(root.val < 10) {
            ans = "0000"+root.val;
        } else if (root.val < 100) {
            ans = "000"+root.val;
        } else if (root.val < 1000) {
            ans = "00"+root.val;
        } else if (root.val < 10000) {
            ans = "0"+root.val;
        } else {
            ans =""+root.val;
        }
        ans+=serialize(root.left);
        ans+=serialize(root.right);
        return ans;
    }

    // Decodes your encoded data to tree.
    String data;
    public TreeNode deserialize(String data) {
          this.data = data;
          return dfs();
    }
    private TreeNode dfs() {
          if(data=="")return null;
          String v = data.substring(0,5);
          data = data.substring(5,data.length());
          if (v.equals("nullP")) return null;
          TreeNode n = new TreeNode(Integer.parseInt(v));
          n.left = dfs();
          n.right = dfs();
          return n;
    }

}
